package cn.alibaba.controller;


import cn.alibaba.pojo.Orders;
import cn.alibaba.pojo.Product;
import cn.alibaba.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OrderService orderService;
    //准备买1件商品
    @GetMapping("/order/prod/{pid}")
    public Orders order(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
//通过restTemplate调用商品微服务
        Product product = restTemplate.getForObject(
                "http://localhost:8001/product/" + pid, Product.class);
        log.info(">>商品信息:" + product);
        Orders order = new Orders();
        order.setProductId(product.getId());
        order.setPayAmount(product.getPrice());
        orderService.createOrder(order);
        return order;

    }
}
